Public Class frmCUSTOMER_DETAIL
    Public CUS_CODE As String = ""
    Public dr_customer As DataRowView = Nothing
    Dim index As Integer = 1

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub frmCUSTOMER_DETAIL_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Try
            GC.Collect()
            Me.Dispose()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub frmCUSTOMER_DETAIL_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        PublicFunction.NewFormLoad(Me)
        Me.Width = 800
        Me.Height = 600
        Me.StartPosition = FormStartPosition.CenterScreen
        If Not dr_customer Is Nothing Then
            txtCus_code.Enabled = False
            txtCus_code.Text = dr_customer.Item("CUS_CODE").ToString
            txtCus_Name.Text = dr_customer.Item("CUS_NAME").ToString
            txtEmail.Text = dr_customer.Item("EMAIL").ToString
            txtID_Card.Text = dr_customer.Item("ID_CARD").ToString
            txtCard_Number.Text = dr_customer.Item("CARD_NUMBER").ToString
            txtPhone1.Text = dr_customer.Item("PHONE1").ToString
            txtPhone2.Text = dr_customer.Item("PHONE2").ToString
            txtRemark.Text = dr_customer.Item("REMARK").ToString
            txtAddress.Text = dr_customer.Item("ADDRESS").ToString
            txtBirthday.Text = dr_customer.Item("BIRTHDAY").ToString
            Dim Gender As Integer = dr_customer.Item("GENDER")
            Select Case Gender
                Case 0
                    ckFemale.Checked = True
                Case 1
                    ckMale.Checked = True
                Case 2
                    ckOther.Checked = True
            End Select
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            Dim Gender As Integer = 0
            If ckFemale.Checked Then
                Gender = 0
            ElseIf ckMale.Checked Then
                Gender = 1
            Else
                Gender = 2
            End If

            Dim Birthday As Date

            If txtBirthday.Text = "" Then
                Birthday = Date.MinValue
            Else
                Try
                    Birthday = CDate(txtBirthday.Text)
                Catch ex As Exception
                    SystemFunction.ShowMessageForm("BIRTHDAY IS WRONG FORMAT!", "")
                    Exit Sub
                End Try
            End If


            Dim OccupationCode As String = cbOccupation.SelectedValue
            If OccupationCode Is Nothing Then OccupationCode = ""
            If CUS_CODE = "" Then
                Dim arName As New ArrayList
                arName.Add("@CUS_CODE")
                arName.Add("@CUS_NAME")
                arName.Add("@ADDRESS")
                arName.Add("@PHONE1")
                arName.Add("@PHONE2")
                arName.Add("@CARD_NUMBER")
                arName.Add("@ID_CARD")
                arName.Add("@BIRTHDAY")
                arName.Add("@GENDER")
                arName.Add("@OCCUPATION_CODE")
                arName.Add("@EMAIL")
                arName.Add("@CREATEDUSER")
                Dim arVal As New ArrayList
                arVal.Add(txtCus_code.Text)
                arVal.Add(txtCus_Name.Text)
                arVal.Add(txtAddress.Text)
                arVal.Add(txtPhone1.Text)
                arVal.Add(txtPhone2.Text)
                arVal.Add(txtCard_Number.Text)
                arVal.Add(txtID_Card.Text)
                If Birthday = Date.MinValue Then
                    arVal.Add(DBNull.Value)
                Else
                    arVal.Add(Birthday)
                End If
                arVal.Add(Gender)
                arVal.Add(OccupationCode)
                arVal.Add(txtEmail.Text)
                arVal.Add(PublicVariable.UserCode)
                Dim dt As DataTable
                dt = BLL.ObjectGetBy("CUSTOMER_ADD", arName, arVal)
                dt.TableName = "CUSTOMER"
                If dt.Rows.Count > 0 Then
                    'BLL.ReturnXML("CUSTOMER", dt)
                    SystemFunction.ShowMessageForm("CREATE CUSTOMER SUCCESSFUL!", "")
                    txtCus_code.Enabled = False
                    CUS_CODE = txtCus_code.Text
                Else
                    SystemFunction.ShowMessageForm("CREATE CUSTOMER ERROR!", "")
                End If
            Else
                Dim arName As New ArrayList
                arName.Add("@CUS_CODE")
                arName.Add("@CUS_NAME")
                arName.Add("@ADDRESS")
                arName.Add("@PHONE1")
                arName.Add("@PHONE2")
                arName.Add("@CARD_NUMBER")
                arName.Add("@ID_CARD")
                arName.Add("@BIRTHDAY")
                arName.Add("@GENDER")
                arName.Add("@OCCUPATION_CODE")
                arName.Add("@EMAIL")
                arName.Add("@UPDATEDUSER")
                Dim arVal As New ArrayList
                arVal.Add(txtCus_code.Text)
                arVal.Add(txtCus_Name.Text)
                arVal.Add(txtAddress.Text)
                arVal.Add(txtPhone1.Text)
                arVal.Add(txtPhone2.Text)
                arVal.Add(txtCard_Number.Text)
                arVal.Add(txtID_Card.Text)
                If Birthday = Date.MinValue Then
                    arVal.Add(DBNull.Value)
                Else
                    arVal.Add(Birthday)
                End If
                arVal.Add(Gender)
                arVal.Add(OccupationCode)
                arVal.Add(txtEmail.Text)
                arVal.Add(PublicVariable.UserCode)
                Dim dt As DataTable
                dt = BLL.ObjectGetBy("CUSTOMER_UPDATE", arName, arVal)
                dt.TableName = "CUSTOMER"
                If dt.Rows.Count > 0 Then
                    'BLL.ReturnXML("CUSTOMER", dt)
                    SystemFunction.ShowMessageForm("UPDATE CUSTOMER SUCCESSFUL!", "")
                    txtCus_code.Enabled = False
                Else
                    SystemFunction.ShowMessageForm("UPDATE CUSTOMER ERROR!", "")
                End If
            End If


        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

    End Sub

    Private Sub txtCus_code_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCus_code.Enter, txtRemark.Enter, txtPhone2.Enter, txtPhone1.Enter, txtID_Card.Enter, txtEmail.Enter, txtCus_Name.Enter, txtCard_Number.Enter, txtAddress.Enter
        Select Case CType(sender, Janus.Windows.GridEX.EditControls.EditBox).Name
            Case "txtCus_Code"
                index = 1
            Case "txtCus_Name"
                index = 2
            Case "txtCard_Number"
                index = 3
            Case "txtID_Card"
                index = 4
            Case "txtPhone1"
                index = 5
            Case "txtPhone2"
                index = 6
            Case "txtAddress"
                index = 7
            Case "txtEmail"
                index = 8

            Case "txtRemark"
                index = 10
        End Select
    End Sub

    Private Sub Keyboardcontrol1_UserKeyPressed(ByVal sender As System.Object, ByVal e As VINAPOS.KeyboardClassLibrarySmall.KeyboardEventArgs) Handles Keyboardcontrol1.UserKeyPressed
        Select Case index
            Case 1
                txtCus_code.Focus()
            Case 2
                txtCus_Name.Focus()
            Case 3
                txtCard_Number.Focus()
            Case 4
                txtID_Card.Focus()
            Case 5
                txtPhone1.Focus()
            Case 6
                txtPhone2.Focus()
            Case 7
                txtAddress.Focus()
            Case 8
                txtEmail.Focus()
            Case 9
                txtBirthday.Focus()
            Case 10
                txtRemark.Focus()
        End Select
        SendKeys.Send(e.KeyboardKeyPressed)
    End Sub

    Private Sub txtBirthday_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBirthday.Enter
        index = 9
    End Sub

    Private Sub btnSubscription_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubscription.Click
        Dim frm As New frmSUBSCRIPTION
        frm.txtCustomerCode.Text = Me.txtCus_code.Text
        frm.txtCustomerName.Text = Me.txtCus_Name.Text
        frm.ShowDialog()
    End Sub
End Class